﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wf=System.Windows.Forms;
using WPF_DbCenter.CommonMethod;
using XLCS.Common;
using System.Data.SqlServerCe;
namespace WPF_DbCenter.SqlCE.AdditionUI
{
    /// <summary>
    /// Interaction logic for Sdf2Sdf.xaml
    /// </summary>
    public partial class Sdf2Sdf : Window
    {
        public Sdf2Sdf()
        {
            InitializeComponent();
        }

        
        private void butImport_Click(object sender, RoutedEventArgs e)
        {
            string db = textBox1.Text;
            string pwd = passwordBox1.Password;
            if (string.IsNullOrEmpty(db))
            {
                MessageBox.Show("Invalid db");
                return;
            }

            IDbEnginer srcEngine = new CEEnginer();
            srcEngine.OpenDatabase(db,pwd);
            
            srcEngine.CustomHandler.Open(new CoreEA.LoginDbInfo(db, pwd, false));

            IDbEnginer eg = App.MainEngineer;

            if (string.IsNullOrEmpty(((CEEnginer)App.MainEngineer).CurOpeningDbName))
            {
                eg.OpenDatabase(((CEEnginer)App.MainEngineer).CurOpeningDbName);
            }
            else
            {
                eg.OpenDatabase(((CEEnginer)App.MainEngineer).CurOpeningDbName, ((CEEnginer)App.MainEngineer).CurPwd);
            }

            try
            {
                foreach (string item in srcEngine.TableList)
                {
                    CommonUtil.CopyTable(srcEngine, (SqlCeConnection)eg.DbHandler.CreateConnection(), string.Format("Select * from [{0}]", item), item);
                }

                MessageBox.Show("Complete");
            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }

            
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            using (wf.OpenFileDialog of = new System.Windows.Forms.OpenFileDialog())
            {
                of.Filter = "SqlCe Db File(*.sdf)|*.sdf|All Files(*.*)|*.*";
                if (of.ShowDialog() == wf.DialogResult.OK)
                {
                    textBox1.Text = of.FileName;
                }
            }
        }
    }
}
